package com.xyw.code.taobaokeapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xyw.code.taobaokeapi.service.TbkService;
import com.xyw.code.taobaokeapi.service.WeChatService;
import com.xyw.code.taobaokeapi.util.TbkUtil;
import com.xyw.code.taobaokeapi.util.WeChatUtil;
import com.xyw.common.taobaokecommon.enums.WeChatConstant;
import com.xyw.common.taobaokecommon.vo.ArticleItemVO;
import com.xyw.common.taobaokecommon.vo.CommissionInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/8/7
 * Time: 上午11:20
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Service
@Slf4j
public class WeChatServiceImpl implements WeChatService {

    @Autowired
    private TbkService tbkService;

    @Override
    public String processRequest(HttpServletRequest request) {
        // xml格式的消息数据
        String respXml = null;
        // 默认返回的文本消息内容
        String respContent;
        try {
            // 调用parseXml方法解析请求消息
            Map<String,String> requestMap = WeChatUtil.parseXml(request);
            // 消息类型
            String msgType = (String) requestMap.get(WeChatConstant.MsgType);
            String mes = null;
            // 文本消息
            if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_TEXT)) {
                List<ArticleItemVO> items = new ArrayList<>();
                mes =requestMap.get(WeChatConstant.Content).toString();
                if(TbkUtil.isTkl(mes)){
                    CommissionInfoVO commissionInfoVO =
                            tbkService.getCommissionInfo(mes);

                    respXml =  WeChatUtil.sendTextMsg(requestMap, tbkService.covertTkl(commissionInfoVO));
                }else if("我的信息".equals(mes)){
                    String name = requestMap.get(WeChatConstant.FromUserName);
                    Map<String, String> userInfo = new HashMap<>();
                    System.out.println(userInfo.toString());
                    String nickname = userInfo.get("nickname");
                    String city = userInfo.get("city");
                    String province = userInfo.get("province");
                    String country = userInfo.get("country");
                    String headimgurl = userInfo.get("headimgurl");
                    ArticleItemVO item = new ArticleItemVO();
                    item.setTitle("你的信息");
                    item.setDescription("昵称:"+nickname+" 地址:"+country+" "+province+" "+city);
                    item.setPicUrl(headimgurl);
                    item.setUrl("http://www.baidu.com");
                    items.add(item);

                    respXml = WeChatUtil.sendArticleMsg(requestMap, items);
                }
            }
            // 图片消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_IMAGE)) {
                respContent = "您发送的是图片消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 语音消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_VOICE)) {
                respContent = "您发送的是语音消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 视频消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_VIDEO)) {
                respContent = "您发送的是视频消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 地理位置消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_LOCATION)) {
                respContent = "您发送的是地理位置消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 链接消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_LINK)) {
                respContent = "您发送的是链接消息！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 事件推送
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_EVENT)) {
                // 事件类型
                String eventType = (String) requestMap.get(WeChatConstant.Event);
                // 关注
                if (eventType.equals(WeChatConstant.EVENT_TYPE_SUBSCRIBE)) {
                    respContent = "谢谢您的关注！";
                    respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
                }
                // 取消关注
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_UNSUBSCRIBE)) {
                    // TODO 取消订阅后用户不会再收到公众账号发送的消息，因此不需要回复
                }
                // 扫描带参数二维码
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_SCAN)) {
                    // TODO 处理扫描带参数二维码事件
                }
                // 上报地理位置
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_LOCATION)) {
                    // TODO 处理上报地理位置事件
                }
                // 自定义菜单
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_CLICK)) {
                    // TODO 处理菜单点击事件
                }
            }
            mes = mes == null ? "不知道你在干嘛" : mes;
            if(respXml == null)
                respXml = WeChatUtil.sendTextMsg(requestMap, mes);
            System.out.println(respXml);
            return respXml;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
